All files / domain/models User.js

0% Statements 0/22
0% Branches 0/16
0% Functions 0/5
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                                                                                                                                         
/**
 * User Domain Model
 * Pure domain entity with business logic validation
 */
class User {
  constructor(id, name, email, password, createdAt, updatedAt) {
    this.id = id;
    this.name = name;
    this.email = email;
    this.password = password;
    this.createdAt = createdAt;
    this.updatedAt = updatedAt;
  }
 
  /**
   * Factory method to create a new User
   * @param {Object} data - { name, email, password }
   * @returns {User}
   */
  static create(data) {
    const { name, email, password } = data;
 
    // Validation
    if (!name || name.trim().length === 0) {
      throw new Error('User name is required');
    }
 
    if (!email || !User.isValidEmail(email)) {
      throw new Error('Valid email is required');
    }
 
    if (!password || password.length < 6) {
      throw new Error('Password must be at least 6 characters long');
    }
 
    return new User(null, name.trim(), email.toLowerCase(), password, new Date(), new Date());
  }
 
  /**
   * Email validation
   * @param {string} email 
   * @returns {boolean}
   */
  static isValidEmail(email) {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
  }
 
  /**
   * Update user name
   * @param {string} newName 
   */
  updateName(newName) {
    if (!newName || newName.trim().length === 0) {
      throw new Error('User name is required');
    }
    this.name = newName.trim();
    this.updatedAt = new Date();
  }
 
  /**
   * Get user without sensitive data
   * @returns {Object}
   */
  toPublicJSON() {
    return {
      id: this.id,
      name: this.name,
      email: this.email,
      createdAt: this.createdAt,
      updatedAt: this.updatedAt
    };
  }
}
 
module.exports = User;